package easy;

public class Solution_LCP_29 {

    public int orchestraLayout(int num, int xPos, int yPos) {
        int r_len = Math.min(xPos, num - xPos - 1);
        int c_len = Math.min(yPos, num - yPos - 1);
        int len = Math.min(r_len, c_len);
        long count = (num - 1 + num - 1 - 2L * (len - 1)) * len / 2;
        count = (count % 9) * 4 % 9;
        int num_new = num - len - len;
        if (len == xPos) {
            count += ((yPos - len + 1) % 9);
        } else if (len + num_new - 1 == yPos) {
            count += ((num_new - 1) % 9);
            count += ((xPos - len + 1) % 9);
        } else if (len + num_new - 1 == xPos) {
            count += ((num_new - 1) % 9);
            count += ((num_new - 1) % 9);
            count += (num_new + len - yPos);
        } else if (len == yPos) {
            count += ((num_new - 1) % 9);
            count += ((num_new - 1) % 9);
            count += ((num_new - 1) % 9);
            count += (len + num_new - xPos);
        }
        count %= 9;
        return count == 0 ? 9 : (int) count;
    }

    public static void main(String[] args) {
        Solution_LCP_29 model = new Solution_LCP_29();
        System.out.println(model.orchestraLayout(4, 2, 2)); // 6
//        System.out.println(model.orchestraLayout(1, 0, 0));
        System.out.println(model.orchestraLayout(2511, 1504, 2235)); //3
        System.out.println(model.orchestraLayout(2511, 275, 275)); // 9
//        System.out.println(model.orchestraLayout(7466, 7084, 2520));
        System.out.println(model.orchestraLayout(971131546, 966980466, 531910024)); //3
        System.out.println(model.orchestraLayout(971131546, 4151179, 966980466)); //2
        System.out.println(model.orchestraLayout(702533400, 336774581, 230291515)); //8
    }
}
